package com.bw.home

import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.bw.home.entity.CommentEntity
import com.bw.home.entity.SimpleVideoEntity
import com.bw.home.intent.HomeIntent
import com.bw.home.viewmodel.HomeViewModel
import com.bw.lib_miv.IUiState
import com.bw.lib_miv.RequestType
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch


@Composable
fun CommendPage(
    entity: SimpleVideoEntity,
    vm: HomeViewModel = hiltViewModel(),
    scope: CoroutineScope = rememberCoroutineScope()
) {

    val list = remember {
        SnapshotStateList<CommentEntity>()
    }

    LaunchedEffect(key1 = "", block = {
        scope.launch {
            vm.uiState.collect {
                when (it) {

                    is IUiState.OnSuccess -> {
                        if (it.type == RequestType.COMMENT) {
                            list.clear()
                            list.addAll(it.data as Collection<CommentEntity>)
                            list.sortByDescending {
                                it.id
                            }
                        }
                    }

                    is IUiState.OnFailed -> {
                        Log.d("TagA", "获取评论失败: ${it.msg}")
                    }

                    else -> {}
                }
            }
        }
        vm.sendIntent(HomeIntent.getComment(0, entity.item_id))
    })


    LazyColumn(
        Modifier
            .fillMaxWidth()
            .padding(5.dp)
    ) {
        items(list) {
            comment(entity = it)
        }
    }

}

@Composable
fun comment(entity: CommentEntity) {

    Row(
        Modifier
            .fillMaxWidth()
            .height(70.dp), verticalAlignment = Alignment.CenterVertically
    ) {

        AsyncImage(
            model = if (entity.userlogo.isNotBlank()) {
                entity.userlogo
            } else {
                "https://ts1.cn.mm.bing.net/th/id/R-C.540e5342727cf2d6b70dd5e133b29349?rik=RKx5B%2bbA%2fvVEVQ&riu=http%3a%2f%2fn.sinaimg.cn%2fsinakd20220929s%2f372%2fw986h986%2f20220929%2fa6ad-3272e94d6244f842d52e71a68cf98da5.jpg&ehk=zonTxMPNHqqDiZr3j%2b7h5tFqHjDPnqTvIxiyzawfy5Y%3d&risl=&pid=ImgRaw&r=0"
            },
            contentDescription = "", modifier = Modifier.size(50.dp)
        )

        Column(
            Modifier
                .fillMaxHeight()
                .padding(start = 7.dp)
                .weight(1f), verticalArrangement = Arrangement.Center
        ) {
            Text(
                text = if (entity.nickname.isNotBlank()) {
                    entity.nickname
                } else {
                    "虚拟用户"
                },
                fontSize = 18.sp
            )

            Text(text = entity.content)
        }

    }

}